/**
 * Created by gongxun on 16/11/18.
 */

var MainScene = cc.Scene.extend({
    sendHHtime : null,                      //心跳发送时间
    mLoginTimeOut : null,                   //登录超时间隔
    mWebscoketResetCount : null,            //断线重练失败次数
    mHHTime : null,                         //发送心跳间隔
    mBreakTime : null,                      //心跳没收到触发间隔
    mWsCloseReconTime : null,               //ws断开后开始重练间隔
    mUserOnlineTime : null,                 //用户在线状态刷新

    mIsNetOnline : null,                    //检查网路状态参数 下次启动检测函数时 是false 触发断线重练
    mCheckOfflineTime : null,               //检测网络状态时间
    ctor : function(){
        this._super();
        mo.mainScene = this;
        this.mLoginTimeOut = 5;
        this.mWebscoketResetCount = 0;
        this.mHHTime = 1;
        this.mBreakTime = 3;
        this.mWsCloseReconTime = 1;
        this.mUserOnlineTime = 5;

        this.mCheckOfflineTime = 4;

        this.addChild(new SceneMainLayer());

        //初始化
        mo.gameMsgManager.init();

        //初始化包房帮助
        mo.gameRoomRuleHelper.init();

        //初始化本地表情排序
        mo.gameUserInfo.loadUserCostomRate();

        mo.eventHelper.add("websocketopen", this, function(){
            //连接成功
            this.schedule(this.onSendHH, this.mHHTime);
            this.schedule(this.onSendUserOnline, this.mUserOnlineTime);
        });

        mo.eventHelper.add("websocketclose", this, function(){
            //连接失败
            this.unschedule(this.onSendHH);
            this.unschedule(this.onSendUserOnline);

            this.setStartBreak();

            if (!mo.gameWebsocket.mUesClose){
                mo.showWaitLayerNotVisible();
            }
        });

        mo.eventHelper.add("HHResponse", this, function(){
            this.setHHResponseTime();
        });

        //加载native
        if (cc.sys.isNative){
            var gt = "";
            if (cc.NativeHelper != null)
            {
                mo.nativeHelper = cc.NativeHelper.singleton();
                gt = mo.nativeHelper.getHello();

                mo.nativeConfig = cc.NativeConfig.singleton();

            }
            if (gt != "gt"){
                cc.director.end();
            }

            //初始化
            mo.nativeConfig.setHost(mo.gameHelper.htmlHost);
            mo.nativeConfig.setAppstoreUrl(mo.gameHelper.appHomeUrl);
            mo.nativeConfig.setAndroidDownUrl(mo.gameHelper.androidDownUrl);
            mo.nativeConfig.setRoomMsgFunc(mo.showRoomMsgShow);
            mo.nativeConfig.setWaitLayerFunc(mo.showWaitLayer, mo.closeWaitLayer);

            //本地版本没有更新 获取设备信息
            if (typeof mo.nativeHelper.getPhoneDeviceInfo != typeof undefined){
                // mo.gameUserInfo.deviceInfo = mo.nativeHelper.getDeviceInfo();
                mo.nativeHelper.getPhoneDeviceInfo(mo.gameUserInfo.setPhoneDeviceInfo);
            }
            //获取经纬度
            this.getPhoneLocationBase();
            //外部打开的通知
            if (typeof mo.nativeConfig.setExternalOpenCall != typeof undefined){
                mo.nativeConfig.setExternalOpenCall(mo.gameUserInfo.setExternalOpen);
                mo.gameUserInfo.setExternalOpen(mo.nativeConfig.getExternalOpen());
            }
            //获取ip
            mo.httpClient.getClientIp(function(isSuccess, ip){
                if (isSuccess){
                    cc.log("获取到了IP:" + ip);
                }else{
                    cc.log("没有获取到IP");
                }
            });
            //testversion
            if (typeof  mo.nativeConfig.getGameTestVersion != typeof undefined){
                mo.gameHelper.testVersion = mo.nativeConfig.getGameTestVersion();
            }else{
            }
        }
    },

    onEnter : function() {
        this._super();

        //初始化音乐
        mo.gameUserInfo.loadMusicAndEffects();

        //初始化本地语言
        mo.gameUserInfo.loadLocalism();

        //初始化登录帮助
        mo.loginHelper.init();

        //初始化本地server
        mo.loginHelper.loadLastLoginInfo();

        //加入房间的结果
        mo.eventHelper.add("msg_joinFreeTableStatus", this, function(isEnter){
            if (isEnter === false){
                if (mo.sceneHelper.currentLayerName == "login"){
                    mo.sceneHelper.intoHallLayer();
                }else{
                    if (mo.endInfoLayer != null && mo.endInfoLayer.isVisible() == true){
                        mo.sceneHelper.intoHallLayer();
                    }
                }

            }
            mo.sceneHelper.closeEndInfoLayer();
        });

        //进入游戏结果
        var that = this;
        cc.eventManager.addCustomListener(cc.game.EVENT_HIDE, function(event){
            mo.soundHelper.onGameHide();
        });

        cc.eventManager.addCustomListener(cc.game.EVENT_SHOW, function(event){
            that.stopCheckOnline();
            that.runAction(cc.sequence(
                cc.delayTime(1),
                cc.callFunc(function () {
                    mo.soundHelper.onGameShow();
                }, that)
            ))
        });
    },

    onExit : function(){
        this._super();
    },

    //发送心跳
    onSendHH : function(dt){
        this.sendHHtime = mo.gameHelper.getMillisecond();
        this.unschedule(this.setHHResponseTimeout);
        this.scheduleOnce(this.setHHResponseTimeout, this.mBreakTime);

        // cc.log("发送心跳");
        mo.gameMsgManager.sendHH();
    },

    //发送用户在线状态
    onSendUserOnline : function(dt){
        // cc.log("发送用户在线状态");
        mo.gameMsgManager.sendCheckUserState();
    },

    //触发close重连
    setStartBreak : function(){
        if(!mo.gameWebsocket.mUesClose){
            this.scheduleOnce(this.onBreak, this.getWebsocketReConnectTime());
        }
    },

    //close重连
    onBreak : function(dt){
        mo.loginHelper.reConnet();
    },

    //心跳返回
    setHHResponseTime : function(){
        this.unschedule(this.setHHResponseTimeout);

        var delay = mo.gameHelper.getMillisecond() - this.sendHHtime;
        delay /= 2;
        if (delay > 100){
            cc.log("................延迟%d(毫秒)...........", delay);
        }
        mo.gameUserInfo.gameDealytime = delay;

        mo.eventHelper.dispatch("gameDealyTimeUpdate");
    },

    //断线重练
    setHHResponseTimeout : function(dt){
        this.setHHResponseTime();

        // cc.log("触发心跳断线重连");

        //断线重连
        // mo.gameWebsocket.reconnectClose();
    },

    //启动断线重练检测
    startCheckOnline : function(){
        this.unschedule(this.onCheckOffline);
        this.scheduleOnce(this.onCheckOffline, this.mCheckOfflineTime);
    },

    //停止断线重练检测
    stopCheckOnline : function(){
        this.unschedule(this.onCheckOffline);
    },

    //检测断线重练
    onCheckOffline : function(dt){
        cc.log("没有收到消息触发断线重连");
        mo.gameWebsocket.reconnectClose();
    },

    //login超时检测
    onLoginTimeout : function(){
        this.scheduleOnce(this.onLoginTimeoutUpdate, this.mLoginTimeOut);
    },

    //login超时回调
    onLoginTimeoutUpdate : function(dt){
        mo.gameWebsocket.reconnectClose();
    },

    //关闭login超时回调
    closeLoginTimeout : function(){
        this.unschedule(this.onLoginTimeoutUpdate);
    },

    //返回超时时间
    getWebsocketReConnectTime : function(){
        return this.mWsCloseReconTime;
        // if (this.mWebscoketResetCount < 3){
        //     return 2;
        // }else{
        //     return 3;
        // }
        // this.mWebscoketResetCount += 1;
    },

    //底包方法
    getPhoneLocationBase : function(){
        if (cc.sys.isNative){
            //获取经纬度
            if (typeof mo.nativeHelper.getPhoneLocation != typeof undefined){
                mo.nativeHelper.getPhoneLocation(mo.gameUserInfo.setPhoneLocation);
            }
        }
    }
});

var SceneMainLayer = cc.Layer.extend({
    ctor : function(){
        this._super();
        mo.sceneMainLayer = this;
    }
});

mo.nativeHelper = null;